<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Upload</title>
    <script src="jquery-2.1.0.js"></script>
    <script>
        $(document).ready(function() {
            $('#uploadForm').on('submit', function(e) {
                e.preventDefault(); // 阻止表单的默认提交行为

                var formData = new FormData(this); // 创建一个FormData对象来包含表单数据

                $.ajax({
                    type: 'POST',
                    url: '/jianhuang/jh', // 指向你的Spring Boot端点的URL
                    data: formData,
                    processData: false, // 告诉jQuery不要去处理发送的数据
                    contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                    success: function(result) {
                        // var data = new FormData($("#uploadForm")[0]);
                        if (result.code === 200) {
                            alert('File processed successfully. Is Yellow: ' + result.data);
                            if (result.data===true){
                                alert("这是一个黄色图片")
                            }else {
                                alert("这是一个正常图片")
                                var data = new FormData($("#uploadForm")[0]);
                                alert(data)
                                $.ajax({
                                    url:"/upload/multipartFile",
                                    type:"post",
                                    dataType:"json",
                                    data:data,
                                    // cache: false,
                                    processData: false,
                                    contentType: false,
                                    success:function(res){
                                        // alert("成功");
                                        alert(res.data)
                                    }
                                })
                            }
                        } else {
                            alert('Error: ' + result.message);
                        }
                    },
                    error: function(xhr, status, error) {
                        alert('An error occurred: ' + error);
                    }
                });
            });
        });
        // 鉴黄视频
        function uploadVideo() {
            const formData = new FormData(document.getElementById('videoUploadForm'));

            fetch('/jianhuang/jhsp', {
                method: 'POST',
                body: formData
            })
                .then(response => response.json())
                .then(data => {
                    const resultDiv = document.getElementById('result');
                    resultDiv.textContent = data.msg;

                    if (!data.data) {
                        // 显示错误或警告
                        resultDiv.classList.add('error');
                    } else {
                        // 显示成功消息
                        resultDiv.classList.add('success');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    const resultDiv = document.getElementById('result');
                    resultDiv.textContent = 'An error occurred while uploading the video.';
                    resultDiv.classList.add('error');
                });
        }
    </script>
</head>
<body>
<h1>File Upload</h1>
<form id="uploadForm" method="post" action="/upload/multipartFile" enctype="multipart/form-data">
    <!--    required  必填-->
    <input type="file" name="multipartFile" required>
    <button type="submit">Upload</button>
</form>
<hr>
<h1>Upload Video to Check for Pornographic Content</h1>
<form id="videoUploadForm" enctype="multipart/form-data">
    <input type="file" name="multipartFile" id="multipartFile" accept="video/*" required>
    <button type="button" onclick="uploadVideo()">Upload</button>
</form>

<div id="result"></div>
</body>
</html>